package vno.fux.res.manager
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import vno.fux.res.pool.MapResource;
	
	public class CommonLoader extends EventDispatcher
	{
		public static var PIC:String 			= "pic";
		public static var SOUND:String			= "sound";
		public static var XML:String 			= URLLoaderDataFormat.TEXT;
		public static var TEXT:String 			= URLLoaderDataFormat.TEXT;
		public static var BINARY:String 		= URLLoaderDataFormat.BINARY;
		public static var VARIABLES:String		= URLLoaderDataFormat.VARIABLES

		private var _imgLoader:ImgLoader;
		private var _dataLoader:DataLoader;
		private var _soundLoader:SoundLoader;
		private var _currentLoader:EventDispatcher;
		private var _id:uint;
		private var _missionId:uint;
		private var _isLoading:Boolean;
		private var _currentMission:ResourceMission;

		
		public function CommonLoader(id_:uint = 0)
		{
			_imgLoader = new ImgLoader;
			_dataLoader = new DataLoader;
			_soundLoader = new SoundLoader;
			_id = id_;
		}
		
		public function load(mission:ResourceMission):void
		{
			_currentMission = mission;
			if(mission.loadType == CommonLoader.PIC)
			{
				addListeners(_imgLoader);
				_imgLoader.load(mission.url, mission.target);
			}
			else if(mission.loadType == CommonLoader.SOUND)
			{
				addListeners(_soundLoader);
				_soundLoader.load(mission.url, mission.target);
			}
			else
			{
				addListeners(_dataLoader);
				_dataLoader.load(mission.url, mission.target, mission.loadType);
			}
			_missionId = mission.id;
			_isLoading = true;
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			super.removeEventListener(type, listener, useCapture);
			remove();
		}
		
		protected function addListeners(loader:EventDispatcher):void
		{
			_currentLoader = loader;
			loader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			loader.addEventListener(Event.COMPLETE,completeHandler);
			loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		
		protected function delListeners(loader:EventDispatcher):void
		{
			loader.removeEventListener(Event.COMPLETE,completeHandler);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			loader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		protected function completeHandler(event:Event):void
		{
			dispatchEvent(new Event(Event.COMPLETE));
			//remove();
		}
		//加载过程
		protected function progressHandler(evt:ProgressEvent):void
		{
			dispatchEvent(evt);
		}
		
		//错误处理
		//fux:need to design a retry mechanism
		protected function ioErrorHandler(evt:IOErrorEvent):void
		{
			trace(evt.type + " at " + this.toString()+" on "+_currentMission.url);
			_isLoading = false;
			dispatchEvent(evt);
			//Alert.show("读取文件出错，未找到文件！");
		}
		
		protected function remove():void
		{
			if(_currentLoader)
			{			
				delListeners(_currentLoader);
				_currentLoader = null;
				_currentMission = null;
				_isLoading = false;	
			}
		}
		
		public function get id():uint
		{
			return _id;
		}
		
		public function set id(id_:uint):void
		{
			_id = id_;
		}

		public function get missionId():uint
		{
			return _missionId;
		}

		public function get currentMission():ResourceMission
		{
			return _currentMission;
		}
		override public function toString():String
		{
			return "CommonLoader:"+_id;
		}

	}
}